package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(String name) {
        return "Hello! " + name;
    }

    @RequestMapping("/login")
    @ResponseBody
    public String Login(String username,String password) {
        return username + " " + password;
    }

    @RequestMapping("/register")
    @ResponseBody
    public Object Register(@RequestBody User user) {
        return user;
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(@RequestParam(value = "time",required = false) String createtime){
        return "time: " + createtime;
    }

    @RequestMapping("/{name}/{age}")
    public String test(@PathVariable String name, @PathVariable String age){
        return "姓名: " + name + " " + "年龄: " + age;
    }

    @Value("${upload.path}")
    private String uploadPath;

    @RequestMapping("/upload")
    public String Upload(@RequestPart("myfile") MultipartFile file) throws IOException {
        // 1. 上传文件目录
            String basePath = uploadPath;
        // 2. 生成动态的文件名
            String fileName = UUID.randomUUID() + (file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".")));
        // 3. 保存文件
            file.transferTo(new File(basePath + fileName));
            return "上传成功";
    }

    @RequestMapping("/getCookie")
    public String GetCookie(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        // 获取所有的 Cookie 信息
        Cookie[] cookies = request.getCookies();
        return name + "你好";
    }

    @RequestMapping("/cookie")
    public String cookie(@CookieValue("bite") String bite) {
        return "cookie: " + bite;
    }

    @RequestMapping("/header")
    public String header(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent: " +  userAgent;
    }

    @RequestMapping("/setsession")
    public String setSess(HttpServletRequest request) {
        // 这里的true 表示, 没有session对象就创建. false就是没有对象不创建
        HttpSession session = request.getSession(true);
        if(session != null){
            session.setAttribute("username","java");
        }
        return "session 存储成功";
    }
    @RequestMapping("/getsession")
    public String getSess(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        String username = "暂无";
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
         }
        return "username: " + username;
    }

    @RequestMapping("/getsession2")
    public String getSess2(@SessionAttribute(value = "username", required = false) String username) {
        return "username: " + username;
    }


}

